package com.huluobo.a2209mdmdemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2209mdmdemo.ui.theme._2209MDMDemoTheme
import com.huluobo.a2209mdmdemo.views.MainPage
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            ), 102
        )
        setContent {
            _2209MDMDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    var startPage = RoutePath.WELCOME
                    val mmkv = MMKV.defaultMMKV()
                    val navController = rememberNavController()
                    //通过mmkv去读取是否进入过app
                    val isIn = mmkv.decodeBool(Const.PARAM_IS_IN, false)
                    if (isIn) {
                        //如果进入过app,则直接跳转到主页面
                        startPage = RoutePath.MAIN
                    } else {
                        //如果首次进入app,则进入欢迎页,然后倒计时5秒后,再次进入首页
                        lifecycleScope.launch {
                            //延迟5秒
                            async { delay(5000) }.await()
                            //跳转至首页
                            navController.navigate(RoutePath.MAIN)
                            //保存是否进入过
                            mmkv.encode(Const.PARAM_IS_IN, true)
                        }
                    }

                    NavHost(navController = navController, startDestination = startPage, builder = {
                        composable(RoutePath.WELCOME) { WelcomePage() }
                        composable(RoutePath.MAIN) { MainPage() }
                    })
                }
            }
        }
    }
}